package com.google.code.hostsman.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseOpenHelper extends SQLiteOpenHelper {

	public DatabaseOpenHelper(Context context) {
		super(context, DatabaseContract.DATABASE_NAME, null,
				DatabaseContract.DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(DatabaseContract.PRAGMA_FOREIGN_KEYS_ON);
		db.execSQL(DatabaseContract.GroupEntry.TABLE_CREATE);
		db.execSQL(DatabaseContract.DomainIPPairEntry.TABLE_CREATE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.w(DatabaseOpenHelper.class.getName(),
				"Upgrading database from version " + oldVersion + " to "
						+ newVersion + ", which will destroy all old data");

		db.execSQL(DatabaseContract.DomainIPPairEntry.TABLE_DELETE);
		db.execSQL(DatabaseContract.GroupEntry.TABLE_DELETE);
		onCreate(db);
	}
}